const execSQL = require("../../db/mysql");

async function add(form) {
    try {
        let sql = "select * from shoppingcart where userID=? and productID=?";
        let sqlParams = [form.userID, form.productID];
        let result = await execSQL(sql, sqlParams);
        if (result.length > 0) {
            result = result[0];
            const data = await update(form);

            result.num = Number(result.num) + Number(form.num);
            return {
                code: 2,
                msg: "添加购物车成功!数量+" + form.num,
                data: result,
            };
        } else {
            let sql = `select salePrice from product where id=${form.productID}`;
            let price = await execSQL(sql);
            price = price[0].salePrice;
            form.price = price;
            sql =
                "insert into shoppingcart(userID,productID,price,num,spec) values(?,?,?,?,?)";
            let sqlParams = [
                form.userID,
                form.productID,
                price,
                form.num === undefined ? 1 : form.num,
                form.spec,
            ];

            const data = await execSQL(sql, sqlParams);
            sql = "select productName from product where id=?";
            sqlParams = [form.productID];
            productName = await execSQL(sql, sqlParams);
            form.productName = productName[0].productName;
            return {
                code: 1,
                msg: "添加购物车成功!",
                data: form,
            };
        }
    } catch (error) {
        return {
            code: 0,
            msg: "添加购物车失败!",
        };
    }
}
async function select(userID) {
    try {
        let sql =
            "select s.id as id,s.productID as productID,pro.productName as productName,pic.url as productImg,s.price as price,s.num as num from shoppingcart as s,product as pro,picture as pic  where s.userID=? and s.productID=pro.id and pro.id=pic.productID and pic.desc='商品展示'";

        let sqlParams = [userID];
        const data = await execSQL(sql, sqlParams);

        return {
            code: 1,
            msg: "获取购物车成功",
            data: data,
        };
    } catch (error) {
        return {
            code: 0,
            msg: "获取购物车失败",
            data: [],
        };
    }
}
async function update(form) {
    try {
        let sql =
            "update shoppingcart set num=? where userID=? and productID=?";
        let sqlParams = [form.num, form.userID, form.productID];
        await execSQL(sql, sqlParams);
        return {
            code: 1,
            msg: "修改购物车商品数量成功!",
        };
    } catch (error) {
        return {
            code: 0,
            msg: "修改购物车商品数量失败!",
        };
    }
}
async function del(body) {
    try {
        let sql = "delete from shoppingcart where productID=? and userID=?";
        let sqlParams = [body.productID, body.userID];

        const data = await execSQL(sql, sqlParams);

        if (data.affectedRows > 0) {
            return {
                code: 1,
                msg: "删除购物车商品成功",
            };
        } else {
            return {
                code: 0,
                msg: "删除购物车商品失败",
            };
        }
    } catch (error) {
        return {
            code: 0,
            msg: "删除购物车商品错误",
        };
    }
}
module.exports = {
    add,
    select,
    update,
    del,
};
